﻿#region MIT Licenze
//The MIT License

//Copyright (c) 2008 Sebastian Faltoni, dotnetfireball.org

//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Windows.Media;
using Fireball.Extensions;
using System.ComponentModel;


namespace Fireball.OpenXML.Word
{

    /// <summary>
    /// The border for a cell
    /// </summary>
    public sealed class ParagraphBorder : OpenXMLEntity
    {

        /// <summary>
        /// The space between the border and the content
        /// </summary>
        public int? BorderSpace
        {
            get;
            set;
        }

        /// <summary>
        /// The size of the border
        /// </summary>
        public int BorderSize
        {
            get;
            set;
        }

        /// <summary>
        /// If this is True the BorderColor
        /// Property is ignored and used an automatic color
        /// </summary>
        public bool AutoBorderColor
        {
            get;
            set;
        }

        /// <summary>
        /// The border color
        /// </summary>
        public Color BorderColor
        {
            get;
            set;
        }

        [DefaultValue(BorderStyles.None)]
        public BorderStyles Style
        {
            get;
            set;
        }

        /// <summary>
        /// The kind of the border
        /// </summary>       
        [DefaultValue(BorderKind.Left)]
        internal BorderKind BorderKind
        {
            get;
            set;
        }

        public ParagraphBorder()
        {
        }

        private void ParseInternal(XElement xBorder)
        {
            this.BorderKind = (BorderKind)Enum.Parse(typeof(BorderKind), xBorder.Name.LocalName, true);

            var val = xBorder.Attribute(NS.WM + "val");

            if (val != null)
            {
                this.Style = (BorderStyles)Enum.Parse(typeof(BorderStyles), val.Value, true);
            }

            var sz = xBorder.Attribute(NS.WM + "sz");

            if (sz != null)
            {
                this.BorderSize = Convert.ToInt32(sz.Value);
            }

            var space = xBorder.Attribute(NS.WM + "space");

            if (space != null)
            {
                this.BorderSpace = Convert.ToInt32(space.Value);
            }

            var color = xBorder.Attribute(NS.WM + "color");

            if (color != null && color.Value != "auto")
            {
                var s = color.Value;
                if (!s.StartsWith("#"))
                    s = "#" + s;
                this.BorderColor = (Color)ColorConverter.ConvertFromString(s);
            }
        }

        public static ParagraphBorder Parse(XElement xBorder)
        {
            var b = new ParagraphBorder();
            b.ParseInternal(xBorder);
            return b;
        }

        public override System.Xml.Linq.XContainer ToXml()
        {
            if (this.Style == BorderStyles.None) return null;

            XElement xBorder = new XElement(NS.WM + this.BorderKind.ToString().ToLower());

            xBorder.Add(new XAttribute(NS.WM + "val", this.Style.ToOffice()));

            xBorder.Add(new XAttribute(NS.WM + "sz", this.BorderSize));

            if (this.BorderSpace != null)
                xBorder.Add(new XAttribute(NS.WM + "space", this.BorderSpace.Value));
            if (!this.AutoBorderColor)
                xBorder.Add(new XAttribute(NS.WM + "color", this.BorderColor.ToHexFormat()));
            else
                xBorder.Add(new XAttribute(NS.WM + "color", "auto"));

            return xBorder;
        }
    }
}

